package com.jyb.util;


import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

public class SendRequest {
	
	private static final String baseUrl="http://10.10.10.158:8080/jinyinbao-web/adminJinyinbao.action";
    public String login(String account,String password){
    	EncoderUtil encoderUtil = new EncoderUtil();
    	HttpClient httpCilent =new DefaultHttpClient();;
        HttpResponse response;
        HttpPost httpPost;
        HttpEntity entity;
    	String result ="{'ticket':'null','code':'1','time':'null','message':'登录成功！','external':'null'}";
    	try {
    		String method="getTicket";
    		String format ="json";
    		String passwordMD5=encoderUtil.EncoderByMd5(password);
    		String sign =encoderUtil.EncoderByMd5("method="+method+"&loginName="+account+"&password="+passwordMD5);
    		System.out.println("passwordMD5==================="+passwordMD5);
    		System.out.println("sign==================="+sign);
            httpPost = new HttpPost(baseUrl);
            JSONObject data = new JSONObject();
            data.put("sign",sign);
            data.put("method", method);
            data.put("format",format);
            data.put("loginName", account);
            data.put("password", passwordMD5);
            httpPost.setEntity(new StringEntity(data.toString()));
            response = httpCilent.execute(httpPost);
            System.out.println("response.getStatusLine().getStatusCode()========"+response.getStatusLine().getStatusCode());
            while(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                entity = response.getEntity();
                StringBuffer sb = new StringBuffer();
                BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
                String s = null;
                while((s = reader.readLine()) != null){
                	System.out.println("==========================s====="+s);
                    sb.append(s);
                }
                System.out.println("==============sb.toString()============s====="+sb.toString());
               return sb.toString();
            }
        } catch (Exception e) {
        	System.out.println(e.toString());
        	return result;
        }
    	return result;
    }
    	
}
